package org.compiere.process;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Properties;
import java.util.TimeZone;
import java.util.logging.Level;

import org.compiere.model.MBPartner;
import org.compiere.util.DB;
import org.compiere.util.Env;
import org.compiere.util.Msg;
import org.eevolution.model.X_HR_Employee;
import org.joda.time.DateTime;

public class DeactivateEmployee extends SvrProcess {
	
	/** Current context	*/
	private Properties m_ctx;
	/** */
	private DateTime currentDate = new DateTime();
	/** */
	private Timestamp dateFrom;
	/** */
	Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
	/**  */
	ArrayList<MBPartner> listBP = new ArrayList<MBPartner>();
	ArrayList<X_HR_Employee> listEmp = new ArrayList<X_HR_Employee>();

	@Override
	protected void prepare() {
		
		log.info("Disabled employee on the date of dismissal");
		m_ctx = Env.getCtx();

		ProcessInfoParameter[] para = getParameter();
		for (int i = 0; i < para.length; i++)	{
			String name = para[i].getParameterName();
			if (name == null); 
				//
			else if (name.compareTo("DateFrom") == 0){
				dateFrom = (Timestamp) para[i].getParameter();
				currentDate = new DateTime(dateFrom.getTime());
			}
			else
				log.log(Level.INFO, "Unknown Parameter: " + name);
		}
		
		currentDate = currentDate.minusDays(1);

	}

	@Override
	protected String doIt() throws Exception {

		StringBuffer employee = new StringBuffer("Employee: ");
		//
		getEmployee();
		
		for(X_HR_Employee emp: listEmp){
			emp.setIsActive(false);
			emp.saveEx();
		}
		
		int n = 0;
		for(MBPartner bp: listBP){
			bp.setIsActive(false);
			bp.saveEx();
			
			n++;
			employee.append(n+"->"+bp.getLastName()+" "+bp.getFirstName().substring(0, 1)+".; ");
		}
		
		return Msg.translate(m_ctx, "Success")+
				". Number of records: "+listBP.size()+
				" on the date: "+currentDate.toString()+
				". "+employee.toString();
	}
	
	private void getEmployee(){
		
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT R.C_BPARTNER_ID FROM HRM_ORDER T \n");
		sql.append("JOIN HRM_EMPLOYEE R ON R.HRM_ORDER_ID=T.HRM_ORDER_ID \n");
		sql.append("WHERE T.HRM_ORDERTYPE='5' \n");
		sql.append("AND T.STARTDATE="+DB.TO_DATE(new Timestamp(currentDate.getMillis()), true)+" \n");
		sql.append("AND R.ISACTIVE='Y'");
		
		try
		{
			pstmt = DB.prepareStatement(sql.toString(), get_TrxName());
			rs = pstmt.executeQuery();	
					
			while (rs.next ()){				
				listBP.add(new MBPartner(m_ctx, rs.getInt(1), get_TrxName()));
				listEmp.add(X_HR_Employee.getHR_Employee(rs.getInt(1), true));
			}

		}
		catch (SQLException e)
		{
			log.log(Level.SEVERE, "product", e);
		}
		finally
		{
			DB.close(rs, pstmt);
			rs = null; pstmt = null;
		}	
	}

}
